﻿'========================================================================
'功能:申請項目參數設定
'作者:shihyu
'日期: 2011/3/4
'========================================================================
Partial Class WebForm_SysParam_ApplyKindMaintain
    Inherits BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                btnInsert.Visible = False
                gvKindType.Visible = False

            End If
            If Not ChkProgramRight(2) Then '新增
                btnInsert.Visible = False

            End If


            If Not ChkProgramRight(2) Then '編輯
                gvKindType.Columns(0).Visible = False
                gv.Columns(0).Visible = False

            End If

            '先將對應的Gridview及Button隱藏
            'gv.Visible = False
            'btnInsertNew.Visible = False
        End If
    End Sub

    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        hiKind.Value = "Insert"

        gvKindType.SelectedIndex = -1
        gvKindType.EditIndex = 0
        gvKindType.PageIndex = 0
        gvKindType.DataBind()

        gv.Visible = False
        btnInsertNew.Visible = False
    End Sub
    Protected Sub btnInsertNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsertNew.Click
        Me.mpSearch.Show()
        hiKindDetail.Value = "InsertNew"

        gv.SelectedIndex = -1
        gv.EditIndex = 0
        gv.PageIndex = 0
   
        gv.DataBind()
    End Sub
    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        gv.EditIndex = -1
        hiKindDetail.Value = ""
    End Sub
    Protected Sub gvKindType_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvKindType.RowCancelingEdit
        gvKindType.EditIndex = -1
        hiKind.Value = ""
    End Sub

    Protected Sub gvKindType_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvKindType.RowCommand
        If e.CommandName = "Select" Then
            gvKindType.EditIndex = -1 '取消編輯模式
            Me.mpSearch.Show()
            '將對應的Gridview顯示
            gv.Visible = True
            hikind_code.Value = e.CommandArgument.ToString()
            btnInsertNew.Visible = ChkProgramRight(2)
        End If

        If e.CommandName = "Edit" Then
            btnInsertNew.Visible = False
            gv.Visible = False
        End If
    End Sub

    Protected Sub gvKindType_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvKindType.RowUpdated
        gvKindType.EditIndex = -1
        hiKind.Value = ""
    End Sub
    Protected Sub gv_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gv.RowUpdated
        gv.EditIndex = -1
        hiKindDetail.Value = ""
    End Sub
    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Me.mpSearch.Show()
        Dim kind_code As String = CType(gv.Rows(e.RowIndex).FindControl("lblikind_code"), Label).Text.Trim
        Dim type_code As String = CType(gv.Rows(e.RowIndex).FindControl("lblitype_code"), Label).Text.Trim
        Dim bbl As Telecom.BBL.ApplyKindBBL = New Telecom.BBL.ApplyKindBBL()
        bbl.DeleteModifykind(dbName, kind_code, Me.ddlsuit_site.SelectedValue.Trim, type_code) '刪除對應資料
    End Sub

    Protected Sub ddlsuit_site_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlsuit_site.SelectedIndexChanged
        hiKind.Value = ""
        gvKindType.EditIndex = -1
        gvKindType.DataBind()

        If gv.Visible = True Then '如果對應之GridView顯示則取消編輯模式
            gv.EditIndex = -1
            gv.DataBind()
        End If
    End Sub

    Protected Sub gvKindType_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvKindType.RowUpdating
        Dim kind_code As String = CType(gvKindType.Rows(e.RowIndex).FindControl("txtkind_code"), TextBox).Text.Trim
        Dim kind_name As String = CType(gvKindType.Rows(e.RowIndex).FindControl("txtkind_name"), TextBox).Text.Trim
        Dim NeedApproveText As String = CType(gvKindType.Rows(e.RowIndex).FindControl("txtNeedApprove"), TextBox).Text.Trim
        Dim NeedApprove As Integer
        '判斷輸入的值是否為數字,若不是將該值預設=7
        If IsNumeric(NeedApproveText) = True Then
            NeedApprove = Int32.Parse(NeedApproveText)
        Else
            NeedApprove = 7
        End If

        '檢查代號是否空白
        If kind_code = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmpty"))
            Exit Sub
        End If

        '檢查名稱是否空白
        If kind_name = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmptytype_name"))
            Exit Sub
        End If

        '檢查簽核層級只可輸入0~6
        If NeedApprove < 0 OrElse NeedApprove > 6 Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrNumber"))
            Exit Sub
        End If

        '判斷代號是否存在
        Dim bbl As Telecom.BBL.ApplyKindBBL = New Telecom.BBL.ApplyKindBBL()
        If CType(gvKindType.Rows(e.RowIndex).FindControl("txtkind_code"), TextBox).Enabled Then
            If bbl.kind_codeIsExist(dbName, ddlsuit_site.SelectedValue, kind_code) Then
                e.Cancel = True
                ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrExist"))
                Exit Sub
            End If
        End If
    End Sub

 
  
  
    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Me.mpSearch.Show()
        Dim kind_code As String = CType(gv.Rows(e.RowIndex).FindControl("lbliekind_code"), Label).Text.Trim
        Dim type_code As String = CType(gv.Rows(e.RowIndex).FindControl("ddlitype_code"), DropDownList).SelectedValue.Trim

        '檢查申請代號是否空白
        If kind_code = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmptykind_code"))
            Exit Sub
        End If

        '檢查申請種類代號是否空白
        If type_code = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmptytype_code"))
            Exit Sub
        End If

        '判斷申請種類代號是否存在
        Dim bbl As Telecom.BBL.ApplyKindBBL = New Telecom.BBL.ApplyKindBBL()
        If kind_code <> "" Then
            If bbl.Modifykind_codeIsExist(dbName, ddlsuit_site.SelectedValue, kind_code, type_code) Then
                e.Cancel = True
                ShowMessageForAjax(btnInsert, GetLocalResourceObject("ApplyErrExist"))
                Exit Sub
            End If
        End If
    End Sub

    Protected Sub gv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.DataBound
        Me.mpSearch.Show()
    End Sub
End Class
